package com.gton.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.gton.annotation.SwaggerScanClass;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @description: Swagger配置
 * @author: GuoTong
 * @createTime: 2021-10-05 15:14
 * @since JDK 1.8 OR 11
 **/
@Configuration
@EnableWebMvc
@EnableKnife4j
public class SwaggerConfig {

    @Value("${knife4j.local-open:true}")
    private boolean isOpenSwagger;

    /**
     * Description: localhost:8080/doc.html
     * RequestHandlerSelectors选择需要加入Swagger文档的接口扫描
     *
     * @author: GuoTong
     * @date: 2021-10-05 15:15:47
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .enable(isOpenSwagger)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(SwaggerScanClass.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui Doc.html")
                .description("gp Doc文档")
                .termsOfServiceUrl("http://www.guotong.work:8082/")
                .contact(new Contact("郭童", "http://www.guotong.work:8082/", "1054769749@qq.com"))
                .version("1.0.0")
                .build();
    }

}
